<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="com.gzgi.framework.util.*"%>
<%@ page import="com.gzgi.framework.context.*"%>
<%@ page import="com.gzgi.framework.resource.model.*"%>
<%@ page import="com.gzgi.framework.resource.service.*"%>
<%@ page import="com.gzgi.framework.resource.util.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%
      Map params = RequestUtil.getParameterMap(request);
	  System.out.println("params:"+params);
      try {  
		    Collection  dataFiles = (Collection) request.getSession().getAttribute(ResourceConstant.RES_SESSION_KEY);
			if(dataFiles == null){
				dataFiles = new HashSet();
			}
			 System.out.println("dataFiles:"+dataFiles);
            String actionType = request.getParameter("actionType");
			String type = request.getParameter("type");
			if("upload".equals(actionType)){
				DiskFileUpload fu = new DiskFileUpload();   
				fu.setSizeMax(20480000); 
				fu.setSizeThreshold(8192); 
				fu.setRepositoryPath(ApplicationContext.getAppPath()+ "/WEB-INF/temp/"); // 设置临时目录   
				List fileItems = fu.parseRequest(request);  
				Iterator iter = fileItems.iterator();   
				 while (iter.hasNext()) {   
					FileItem fileItem = (FileItem) iter.next();   
					String filename = fileItem.getName();// 获得文件名，这个文件名包括路径：   
					if (filename != null) {   
						// 在这里可以记录用户和文件信息   
						if(filename.indexOf("/") != -1){
							filename = filename.substring(filename.lastIndexOf("/")+1, filename.length());
						} else if(filename.indexOf("\\") != -1){
							filename = filename.substring(filename.lastIndexOf("\\")+1, filename.length());
						}
						DataFile dataFile = new DataFile();
						dataFile.setFileId(UUID32.getUUID());
						dataFile.setFilename(filename);
						dataFile.setName(fileItem.getFieldName());
						dataFile.setContentType(fileItem.getContentType());
						dataFile.setSize(fileItem.getSize());
						dataFile.setType(type);
						fileItem.write(new File(ApplicationContext.getAppPath()+ "/WEB-INF/temp/"+dataFile.getFileId()));   
						dataFiles.add(dataFile);
					}   
				}   
				request.getSession().setAttribute(ResourceConstant.RES_SESSION_KEY, dataFiles);
				response.setContentType("text/html;charset=UTF-8");   
				response.getWriter().print("{'success':true,'message':'上传成功'}");   
			} else if("remove".equals(actionType)){
                 String fileId = request.getParameter("fileId");
				 if(fileId != null){
					  System.out.println("fileId:"+fileId);
                     Iterator iterator = dataFiles.iterator();
					 while(iterator.hasNext()){
                           DataFile dataFile = (DataFile)iterator.next();
						   System.out.println("dataFile:"+dataFile.getFileId());
						   if(fileId.equals(dataFile.getFileId())){
							   dataFile.setDeleteFlag(1);
							   System.out.println("#remove:"+fileId);
							   break;
						   }
					 }
				 }
			}
         } catch (Exception ex) {   
            ex.printStackTrace();   
            response.getWriter().print("{'success':flase,'message':'失败'}");   
         }   
%>